package piconode.ext;

import piconode.core.operator.ActivationFunctionWithDerivative;

/*
 * Created on 6 janv. 2005
 * bredeche(at)lri(point)fr
 * 
 */

public class ActivationFunction_LogisticSigmoid extends ActivationFunctionWithDerivative {

	/*
	 * Data
	 */

	protected static double _sigmoidDefaultKValue = 1; // default parameter
	// value
	private double _sigmoidKValue; // more or less flat sigmoid (->0) ou

	// "threshlold" (parameter "k")

	/*
	 * Constructor(s)
	 */

	public ActivationFunction_LogisticSigmoid() {
		_sigmoidKValue = getDefaultSigmoidKValue();
	}

	public ActivationFunction_LogisticSigmoid(double __value) {
		_sigmoidKValue = __value;
	}

	/*
	 * Method(s)
	 */

	static protected double getDefaultSigmoidKValue() {
		return (_sigmoidDefaultKValue);
	}

	@Override
	public double applyFunction(double __value) // f(x) = 1 / ( 1 + e^(-k*x)) [
	// sigmoid function - f(x) is
	// [0,1] ]
	{
		return (1 / (1 + Math.exp(-_sigmoidKValue * __value)));
	}

	/**
	 * derivative of activation function WARNING: input parameter is
	 * f_activation(x) (i.e. f_sigmoide(x)), not x before activation!!! (thanks
	 * to this trick, this function is soooo fast to compute)
	 */
	@Override
	public double applyDerivativeFunction(double __sigmoidValue) // f'(x) =
	// f(x) * (
	// 1 - f(x)
	// ) --
	// WHICH
	// MEANS
	// HERE WE
	// TAKE
	// Factivation(X)
	// as input
	{
		return (this._sigmoidKValue * __sigmoidValue * (1 - __sigmoidValue));
	}

}
